<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @package Framework03
 * @author Walter Tamboer
 */

// Let's get the root directory, this will be useful when setting all the paths.
$root = dirname(dirname(__FILE__));

// The include paths so that all application items can be found.
set_include_path(get_include_path() .
    PATH_SEPARATOR . $root . '/../../library' .
    PATH_SEPARATOR . $root . '/application' .
    PATH_SEPARATOR . $root . '/application/models');

// Make sure that PHP actually shows the errors.
ini_set('display_errors', true);

// Also make sure that PHP shows startup errors.
ini_set('display_startup_errors', true);

// We want to show all errors including strict errors because we don't make mistakes.
error_reporting(E_ALL | E_STRICT);

// We want automatic loading of classes because we're lazy.
require_once 'FW/Loader.php';
FW_Loader::startAutoLoading();

try
{
    // Load in the settings of this website, make sure to change the section when you're going live.
    $config = new FW_Config_Xml($root . '/application/data/config.xml', 'development');

    // Set the path to the log file. Make sure it is chmodded correctly
    FW_Log::setLogFile($root . '/application/data/error.log');

    // Set the requested timezone. Sets the default timezone used by all date/time functions in a script
    date_default_timezone_set($config->timezone);

    // Let's create a connection with the database.
    $db = FW_Database::factory($config->database->adapter, $config->database);
    FW_Database_Table::setAdapter($db);

    // Let's get an instance of the front controller.
    $frontController = FW_Controller_Front::singleton();

    // Sets the path of where the controllers can be found.
    $frontController->setControllerDirectory($root . '/application/controllers');
    
    // Set the default controller.
    $frontController->setDefaultController($config->defaultController);

    // Throw exceptions if needed, if the setting isn't set, we always throw. Bug free is holy.
    $throwExceptions = isset($config->throwExceptions) ? $config->throwExceptions : true;
    $frontController->setThrowExceptions($throwExceptions);

    // Register the needed plugins.
    $frontController->registerPlugin(new FW_Controller_Plugin_DisableMagicQuotes());

    // Dispatch!
    $frontController->dispatch();
}
catch (Exception $e)
{
	echo 'Exception: ' . $e->getMessage();
	FW_Log::write("Exception : '%s'", $e->getMessage());
}
